<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>查询结果页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            width: 80%;
            margin: 100px auto;
            padding: 20px;
            background: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 4px;
        }

        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        table, th, td {
            border: 1px solid #ddd;
        }

        th, td {
            padding: 8px;
            text-align: left;
        }

        th {
            background-color: #f2f2f2;
        }

        .button-container {
            display: flex;
            justify-content: center;
            gap: 10px;
        }

        button {
            padding: 10px 20px;
            background: #5cb85c;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            display: block;
            margin: 10px auto;
            text-align: center;
            text-decoration: none;
        }

        button:hover {
            background: #4cae4c;
        }

        #back-button {
            padding: 10px 20px;
            background: #5cb85c;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            display: block;
            margin: 20px auto;
            text-align: center;
            text-decoration: none;
        }

        #back-button:hover {
            background: #4cae4c;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>查询结果</h2>
    <table id="results-table">
        <thead>
            <tr>
                <!-- 表头会在 JavaScript 中动态生成 -->
            </tr>
        </thead>
        <tbody>
            <!-- 表格内容会在 JavaScript 中动态生成 -->
        </tbody>
    </table>
    <div class="button-container">
        <button id="chart-button" onclick="goToChartSelection()">查询成绩柱状图</button>
        <button id="back-button" onclick="clearQueryAndReturn()">返回</button>
    </div>

</div>
<script>
    function clearQueryAndReturn() {
        localStorage.removeItem('userQuery');
        window.location.href = '/test';
    }

    function goToChartSelection() {
            window.location.href = '/charts/';
    }

    document.addEventListener('DOMContentLoaded', function() {
        const data = JSON.parse(localStorage.getItem('queryResults'));
        console.log('queryResults:', data); // 在控制台中输出数据

        if (data && data.data) {
            const tableHead = document.querySelector('#results-table thead tr');
            const tableBody = document.querySelector('#results-table tbody');

            // 生成表头
            data.columns.forEach(column => {
                const th = document.createElement('th');
                th.textContent = column;
                tableHead.appendChild(th);
            });

            // 生成表格内容
            data.data.forEach(row => {
                const tr = document.createElement('tr');
                row.forEach(cell => {
                    const td = document.createElement('td');
                    td.textContent = cell;
                    tr.appendChild(td);
                });
                tableBody.appendChild(tr);
            });

            // 保留存储的数据以便绘图使用
            // localStorage.removeItem('queryResults');
        } else {
            document.querySelector('.container').innerHTML = '<p>没有查询到数据</p>';
            console.error('数据格式不正确:', data);
        }
    });
</script>
</body>
</html>
